﻿angular.module('Caltech').directive('monthlyReport', function () {
    'use strict';
    var monthlyController = function ($scope, Restangular, Constants) {

        var currentYear = new Date;

        $scope.fullName = Constants.accountInfo.fullName;

        $scope.currentDate = new Date;

        $scope.year = currentYear.getFullYear();

        $scope.monthlyReport = {};

        $scope.viewReports = function (year) {
            Restangular.all('reports').customGETLIST(
            'monthlyreports',
            {
                year: year
            }).then(function (data) {
                $scope.monthlyReport = data;
            });
        };

        $scope.sumOther1 = function (reports) {
            var total = 0;
            angular.forEach(reports, function (report, index) {
                if (index != reports.length - 1) {
                    total += parseInt(report.other1);
                }
                else {
                    report.other1 = total;
                }
            });
            return total;
        };

        $scope.sumOther2 = function (reports) {
            var total = 0;
            angular.forEach(reports, function (report, index) {
                if (index != reports.length - 1) {
                    total += parseInt(report.other2);
                }
                else {
                    report.other2 = total;
                }
            });
            return total;
        };

        $scope.sumOther3 = function (reports) {
            var total = 0;
            angular.forEach(reports, function (report, index) {
                if (index != reports.length - 1) {
                    total += parseInt(report.other3);
                }
                else {
                    report.other3 = total;
                }
            });
            return total;
        };

        $scope.sumOther4 = function (reports) {
            var total = 0;
            angular.forEach(reports, function (report, index) {
                if (index != reports.length - 1) {
                    total += parseInt(report.other4);
                }
                else {
                    report.other4 = total;
                }
            });
            return total;
        };

        $scope.sumOther5 = function (reports) {
            var total = 0;
            angular.forEach(reports, function (report, index) {
                if (index != reports.length - 1) {
                    total += parseInt(report.other5);
                }
                else {
                    report.other5 = total;
                }
            });
            return total;
        };

        $scope.sumOE = function (reports) {
            var total = 0;
            angular.forEach(reports, function (report, index) {
                if (index != reports.length - 1) {
                    total += parseInt(report.operationExpenses);
                }
                else {
                    report.operationExpenses = total;
                }
            });
            return total;
        };

        $scope.sumAE = function (reports) {
            var total = 0;
            angular.forEach(reports, function (report, index) {
                if (index != reports.length - 1) {
                    total += parseInt(report.totalAEExpenses);
                }
                else {
                    report.totalAEExpenses = total;
                }
            });
            return total;
        };

        $scope.sumTax = function (reports) {
            var total = 0;
            angular.forEach(reports, function (report, index) {
                if (index != reports.length - 1) {
                    total += parseInt(report.tax);
                }
                else {
                    report.tax = total;
                }
            });
            return total;
        };

        $scope.viewReports($scope.year);


        $scope.save = function (reports) {
            var data = {
                others: [],
                operationExpenses: [],
                other1: [],
                other2: [],
                other3: [],
                other4: [],
                other5: [],
                tax: []
            };
            data.others = $scope.others;
            angular.forEach(reports, function (model, index) {
                if (index != reports.length - 1) {
                    data.operationExpenses.push(model.operationExpenses);
                    data.other1.push(model.other1);
                    data.other2.push(model.other2);
                    data.other3.push(model.other3);
                    data.other4.push(model.other4);
                    data.other5.push(model.other5);
                    data.tax.push(model.tax);
                }
            });
            console.log(data);

            return Restangular.all('reports').post({ year: $scope.year, data: data }).then(function () {
            });
        };
    };

    return {
        restrict: 'E',
        replace: true,
        transclude: true,
        templateUrl: 'Scripts/app/views/_partials/monthlyReport.html',
        controller: monthlyController
    };
});
